package com.suyuan.controller.admin;

import com.github.pagehelper.PageInfo;
import com.suyuan.entity.ProductInfo;
import com.suyuan.entity.StevedoringInfor;
import com.suyuan.service.NewTransitBatchService;
import com.suyuan.service.StevedoringService;
import com.suyuan.service.TransitBatchInfoService;
import com.suyuan.utils.JsonMessage;
import com.suyuan.vo.NewTransitBatchVO;
import com.suyuan.vo.TransitBatchInfoVO;
import com.suyuan.web.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 装卸货控制器
 *
 */
@Controller
@RequestMapping(value = "/admin/stevedoring")
public class StevedoringController {

    @Resource(name = "stevedoringService")
    private StevedoringService stevedoringService;
    //@Resource(name = "transitBatchInfoService")
    //private TransitBatchInfoService transitBatchInfoService;
    @Resource(name = "newTransitBatchService")
    private NewTransitBatchService newTransitBatchService;

    /**
     * 列表/条件查询
     * @return
     */
    @RequestMapping(value = "/list")
    public String list(Integer stevedoringId, Integer productId, Page page, ModelMap model){
        PageInfo info = stevedoringService.list(stevedoringId,productId,page);
        model.addAttribute("stevedoringId",stevedoringId);
        model.addAttribute("productId",productId);
        model.addAttribute("page",info);
        return "stevedoring_list";
    }

    /**
     * 批量删除
     */
    @RequestMapping(value = "/deleteByIds")
    @ResponseBody
    public Object deleteByIds(String ids){
        if(StringUtils.isBlank(ids)){
            return JsonMessage.error("不能为空");
        }
        String[] idArr = null;
        if(ids.length() >= 2){
            idArr = ids.split(",");
        }else{
            idArr = new String[]{ids};
        }
        for(String id : idArr){
            stevedoringService.delete(Integer.parseInt(id));
        }
        return JsonMessage.success("删除成功");
    }

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping(value = "add")
    public String add(ModelMap model){
        //物流批次编号
        List<NewTransitBatchVO> batches = newTransitBatchService.list();
        //商品编号
        List<ProductInfo> products = stevedoringService.getProductList();
        model.addAttribute("batches",batches);
        model.addAttribute("products",products);
        return "stevedoring_add";
    }

    /**
     * 保存（测试未通过）
     * @return
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Object save(StevedoringInfor stevedoring){
        //设置当前系统时间
        stevedoring.setLoadTime(new Date());
        int code = stevedoringService.save(stevedoring);

        if(code == 1){
            return JsonMessage.success("添加成功");
        }
        return JsonMessage.error("添加失败");
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(Integer id){
        int code = stevedoringService.delete(id);
        if(code == 1){
            return JsonMessage.success("删除成功");
        }
        return JsonMessage.error("删除失败");
    }

    @RequestMapping(value = "/edit")
    public String edit(Integer id,ModelMap model){
        StevedoringInfor stevedoring = stevedoringService.findById(id);
        //物流批次编号
        List<NewTransitBatchVO> batches = newTransitBatchService.list();
        //商品编号
        List<ProductInfo> products = stevedoringService.getProductList();
        model.addAttribute("batches",batches);
        model.addAttribute("products",products);
        model.addAttribute("stevedoring",stevedoring);
        return "stevedoring_edit";
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(StevedoringInfor stevedoring){
        int code = stevedoringService.update(stevedoring);
        if(code == 1){
            return JsonMessage.success("更新成功");
        }
        return JsonMessage.error("更新失败");
    }
}
